import Enity from "./Enity.js"
import FireProp from "./FireProp.js"
import { enemy02Body,enemy02Body02,boo } from "../image.js"
class Enemy02Enity extends Enity {
    type = 'enemy'
    name = "敌人02"
    score = 30 //击杀后玩家所获得的分数
    hp = 5
    img;//敌人的图片
    v;//移动速度
    time = 0;//存活时间

    //参数
    timer = null;//内置定时器
    movePlan = 1;

    constructor(x, y) {
        super(x, y, 20)
        this.img = {
            body: enemy02Body,
            body02:enemy02Body02,
            boo
        }
        this.v = 1
    }
    draw(ctx) {
        if (this.hp > 0) {
            if(this.movePlan == 1){
                ctx.drawImage(this.img.body, this.x - this.R, this.y - this.R, this.R * 2, this.R * 2)
            }else if(this.movePlan ==2){
                ctx.drawImage(this.img.body02, this.x - this.R, this.y - this.R, this.R * 2, this.R * 2)
            }
            
        } else {
            ctx.drawImage(this.img.boo[this.time % 5], this.x - this.R, this.y - this.R, this.R * 2, this.R * 2)
        }
    }
    behavior = function (context) {
        this.time++
        this.move(context)
        return context
    }
    //该敌人的移动机制
    move(context) {
        if (this.hp <= 0) return
        let player = context.NodeList[1]
        switch (this.movePlan) {
            case 1: {//行动模式一 x坐标在玩家附近，y坐标根据玩家y坐标相对移动
                if (this.x <= player.x) {
                    if (this.x <= player.x - player.R * 0.5 - 90) {
                        this.x += this.v
                    }
                    if (this.x >= player.x + player.R * 0.5 - 90) {
                        this.x -= this.v
                    }
                } else {
                    if (this.x <= player.x - player.R * 0.5 + 90) {
                        this.x += this.v
                    }
                    if (this.x >= player.x + player.R * 0.5 + 90) {
                        this.x -= this.v
                    }
                }

                if (this.y <= player.y - player.R * 0.5 - 100) {
                    this.y += this.v
                }
                if (this.y >= player.y + player.R * 0.5 - 100) {
                    this.y -= this.v
                }
                if (this.nodeDistance(player) <= 150) {
                    this.movePlan = 2
                    this.v = 2
                }
                break;
            }
            case 2: {//行动模式二 朝向玩家位置移动
                if (this.x <= player.x - player.R * 0.5) {
                    this.x += this.v
                }
                if (this.x >= player.x + player.R * 0.5) {
                    this.x -= this.v
                }
                if (this.y <= player.y - player.R * 0.5) {
                    this.y += this.v
                }
                if (this.y >= player.y + player.R * 0.5) {
                    this.y -= this.v
                }
                break;
            }
        }

    }
    isOK(context) {
        if (this.y >= 600) {
            this.OK = false
        }
        if (this.hp <= 0) {
            if (!this.timer) {
                if(!context.gameOver) context.score+=this.score
                //概率掉道具
                if (0.2 >= Math.random()) {
                    context.NodeList.push(new FireProp(this.x, this.y))
                }
                this.timer = setTimeout(() => {
                    this.OK = false
                }, 1000)
            }
        }
        return context
    }
}
export default Enemy02Enity